<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddColumnOrderStrInTaskTable extends AbstractMigration
{
    public function up()
    {
        $this->table('task')
            ->addColumn('order_str', 'string', [
                'null' => false,
                'default' => '',
                'limit' => 276,
                'comment' => '任务表排序字段'
            ])
            ->addIndex(['order_str'], ['name' => 'idx_order_str', 'type' => 'normal'])
            ->save();
        $this->execute("UPDATE task t
            JOIN entity e ON e.id = t.entity_id 
            SET t.order_str = IF(e.module_id =47,CONCAT( e.initial_episode_code, '/', e.showings_number, '/', e.`name` ) ,e.`name`)"
        );
    }

    public function down()
    {
        $this->table('task')
            ->removeIndexByName('idx_order_str')
            ->removeColumn('order_str')
            ->save();
    }
}
